package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type UserJson struct {
	UserName string `form:"userName" json:"userName" xml:"userName"  binding:"required"`
	Age      int    `form:"age" json:"age" xml:"age"  binding:"required"`
}

// 测试服务
func HelloTest(c *gin.Context) {
	c.JSON(http.StatusOK, res(0, "success", "ok"))
}

func UserTest(c *gin.Context) {
	name := c.Param("name")
	id := c.Param("id")
	c.JSON(http.StatusOK, res(0, "", name+id))
}

// form-data
func UserTestPostForm(c *gin.Context) {
	userName := c.PostForm("userName")
	c.JSON(http.StatusOK, res(0, "", userName))
}

// application/json
func UserTestPostJson(c *gin.Context) {
	var userJson UserJson
	err := c.ShouldBindJSON(&userJson)
	if err != nil {
		c.JSON(http.StatusOK, res(0, "", "error"))
		return
	}
	c.JSON(http.StatusOK, res(0, "", userJson))
}
